<template>
  <el-card style="margin-bottom: 5px">
    <el-input clearable @clear="load" style="width: 260px; margin-right: 5px;" v-model="data.name" placeholder="请输入商品名查询" :prefix-icon="Search"></el-input>
    <el-button type="primary" @click="load">查 询</el-button>
    <el-button type="info" @click="reset">重 置</el-button>
  </el-card>
  <el-card style="margin-bottom: 5px">
    <el-button type="primary" @click="headleAdd">新   增</el-button>
  </el-card>
  <el-card>
    <el-table :data="data.tableData" style="width: 100%" :header-cell-style="{ color:'#333',backgroundColor:'#dde8ff'}">
      <el-table-column label="商品图片" width="100">
          <template #default="scope">
            <el-image :preview-src-list="[scope.row.img]" :preview-teleported="true" v-if="scope.row.img" :src="scope.row.img" alt="" style="width: 50px; height: 50px; display: block; border-radius: 5px"/>
          </template>
        </el-table-column>
      <el-table-column prop="name" label="商品名称" />
      <el-table-column prop="adminName" label="上传人" />
      <el-table-column prop="fenlei" label="商品分类" />
      <el-table-column prop="shuliang" label="库存数量/个" />
      <el-table-column prop="jiage" label="销售价格/元" />
      <el-table-column prop="zhongliang" label="单体重量/kg" />
      <el-table-column prop="time" label="创建时间" />
      <el-table-column label="操作" width="100">
        <template #default="scope">
          <el-button type="primary" icon="Edit" circle @click="headleEdit(scope.row)" ></el-button>
          <el-button type="danger" icon="Delete" circle @click="del(scope.row.id)" ></el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <Dialog  v-model="data.formVisible" :form="data.form" @save="load">
</Dialog>
</template>
<script setup>
import {Search} from "@element-plus/icons-vue";
import {reactive} from "vue";
import request from "@/utils/request";
import Dialog from "@/views/Shangpinxiangqing/Dialog.vue"
const data = reactive({
  name: "",
  formVisible: false,
  pageNum: 1,
  pageSize: 5,
  total: 6,
  form:{},
});
const load = () => {
  console.log(data.name)
  request.get('shangpinxiangqing/selectPage',{
    params:{
      pageNum:data.pageNum,
      pageSize:data.pageSize,
      name:data.name
    }
  }).then(res =>{
    data.tableData = res.data.list
  }).catch(err =>{
    ElMessage.error(err)
  })
}
load()
// 重置按钮
const reset = () =>{
  data.name = ''
  load()
}

// 新增按钮
const headleAdd = () =>{
  data.form = {}
  data.formVisible = true
}

// 修改保存按钮
const headleEdit = (row) =>{
  data.form = JSON.parse(JSON.stringify(row)) // 深拷贝
  data.formVisible = true
}

// 删除按钮
const del = (id) =>{
  ElMessageBox.confirm('此操作将永久删除该文件, 是否继续?', '删除确认', {type: 'warning',}).then(res => {
    console.log(id)
    request.delete('/shangpinxiangqing/delete/'+id).then(res =>{
      if(res.code === '200'){
        ElMessage.success('删除成功')
        load()
      }
    }).catch(() => {
      ElMessage.error(res.msg)
    })
  }).catch(() => {
    ElMessage.info('已取消删除')
  });
}

</script>
<style scoped>

</style>